package com.graduation.smartparkingplatform.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Dao.PrivateMapper;
import com.graduation.smartparkingplatform.Pojo.Private;
import com.graduation.smartparkingplatform.Pojo.Space;
import com.graduation.smartparkingplatform.Service.PrivateService;
import com.graduation.smartparkingplatform.Service.SpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrivateServiceImpl implements PrivateService {
  @Autowired SpaceService spaceService;
  @Autowired PrivateMapper privateMapper;

  @Override
  // 获取private列表（分页）
  public PageInfo<Private> getPrivateList(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<Private> privateList = privateMapper.getPrivateList();

    PageInfo<Private> pageInfo = new PageInfo<>(privateList);

    return pageInfo;
  }

  // 获取私有人下的车位列表
  @Override
  public List<Space> getPrivateLists(Integer privateId) {
    List<Space> privateList = privateMapper.getPrivateLists(privateId);
    return privateList;
  }

  // 模糊搜索private（分页）
  @Override
  public PageInfo<Private> searchPrivate(Integer page, Integer count, String privateName) {
    PageHelper.startPage(page, count);

    List<Private> privateList = privateMapper.searchPrivate(privateName);

    PageInfo<Private> pageInfo = new PageInfo<>(privateList);

    return pageInfo;
  }

  // 添加私有车位
  @Override
  public Boolean addPrivate(Private privatee) {
    return privateMapper.addPrivate(privatee);
  }

  // 修改私有车位信息
  @Override
  public Boolean changeInfo(Private privatee) {
    return privateMapper.changeInfo(privatee);
  }

  // 修改私有车位状态
  @Override
  public Boolean changePrivateStatus(Integer privateId, Boolean status) {
    return privateMapper.changePrivateStatus(privateId, status);
  }

  // 删除私有车位
  @Override
  public Boolean deletePrivate(Integer privateId) {
    return privateMapper.deletePrivate(privateId);
  }

  // 绑定停车位
  public Boolean bindingSpace(Integer privateId, Integer spaceId) {
    Boolean isPrivateSpaceId = spaceService.isPrivateSpaceId(spaceId);
    if (!isPrivateSpaceId) {
      Boolean bindingSpace = privateMapper.bindingSpace(privateId, spaceId);
      Boolean changeSpacePrivate = privateMapper.changeSpacePrivate(spaceId);
      return bindingSpace && changeSpacePrivate;
    }
    return false;
  }
}
